using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;

using Oarw.General;
using Oarw.General.Text;
    
namespace ScreenMonkey.Plugin.Clips.Song
{
    public class SongDisplayStyle:TextDisplayStyleBase,IXmlPersist
    {
        public SongDisplayStyle()
        {
            Initialise();
        }

        public SongDisplayStyle(SongDisplayStyle source)
        {
            Initialise();
            Copy(source);
        }

        public void Initialise()
        {
            AddTextItem(new DisplayStyleTextItem("Main Text",true));
            AddTextItem(new DisplayStyleTextItem("Copyright"));

            MainText.Margin = new PaddingF(0.03f, 0.05f, 0.03f, 0.05f);

            Copyright.Font = new Font(FontFamily.GenericSansSerif, 12);
            Copyright.ForeColour = Color.White;
            Copyright.Alignment = ContentAlignment.BottomLeft;
            Copyright.Margin = new PaddingF(0.03f,0.9f,0.03f,0.01f);

            Copyright.Changed += new EventHandler(TextItem_Changed);
            MainText.Changed += new EventHandler(TextItem_Changed);
        }

        void TextItem_Changed(object sender, EventArgs e)
        {
            OnChanged();
        }

        public override void Copy(ITextDisplayStyle source)
        {
            Name = source.Name;
            BackgroundTransparent = source.BackgroundTransparent;
            BackgroundColour = source.BackgroundColour;
            backgroundImage = source.BackgroundImage;
            transitionTime = source.TransitionTime;

            SongDisplayStyle songSource = source as SongDisplayStyle;
            if (songSource != null)
            {
                Copyright.Copy(songSource.Copyright);
                MainText.Copy(songSource.MainText);
            }

            OnChanged();
        }

        private bool transparentBackground = false;

        public override bool BackgroundTransparent
        {
            get { return transparentBackground; }
            set
            {
                transparentBackground = value;
                OnChanged();
            }
        }


        private Color backgroundColour = Color.Black;

        public override Color BackgroundColour
        {
            get { return backgroundColour; }
            set
            {
                backgroundColour = value;
                OnChanged();
            }
        }

        private string backgroundImage = "";

        public override string BackgroundImage
        {
            get { return backgroundImage; }
            set { 
                backgroundImage = value;
                OnChanged();
            }
        }

        private int transitionTime=250;

        /// <summary>
        /// The time taken to fade between one slide to the next.This will effect how fast the old words fade out and the new fade in.
        /// The time is in ms where 1000ms will give a transition time of 1 second.
        /// </summary>
        public override int TransitionTime
        {
            get { return transitionTime; }
            set { transitionTime = value; }
        }



        private DisplayStyleTextItem copyright = new DisplayStyleTextItem("Copyright");

        public DisplayStyleTextItem Copyright
        {
            get { return TextItem("Copyright"); }
        }

        private DisplayStyleTextItem mainText = new DisplayStyleTextItem("Main Text");

        public DisplayStyleTextItem MainText
        {
            get { return TextItem("Main Text"); }
        }

        public void SaveXml(System.Xml.XmlWriter Writer)
        {
            Writer.WriteStartElement("style");
            Writer.WriteAttributeString("name", Name);
            Writer.WriteAttributeString("transparent", BackgroundTransparent.ToString());
            Writer.WriteAttributeString("backColour", BackgroundColour.ToArgb().ToString());
            Writer.WriteAttributeString("backImage", BackgroundImage);
            Writer.WriteAttributeString("transitionTime", TransitionTime.ToString());

            SaveTextItem(Writer, MainText, "mainText");
            SaveTextItem(Writer, Copyright, "copyrightText");

            Writer.WriteEndElement();
        }

        public void SaveTextItem(System.Xml.XmlWriter Writer,DisplayStyleTextItem item, string name)
        {
            Writer.WriteStartElement(name);
            Writer.WriteAttributeString("colour", item.ForeColour.ToArgb().ToString());
            Writer.WriteAttributeString("fontFamily", item.Font.FontFamily.Name);
            Writer.WriteAttributeString("fontSize", item.Font.Size.ToString(System.Globalization.CultureInfo.InvariantCulture));
            Writer.WriteAttributeString("fontStyle", item.Font.Style.ToString());
            Writer.WriteAttributeString("alignment", item.Alignment.ToString());
            Writer.WriteAttributeString("margin", item.Margin.ToString());

            Writer.WriteStartElement("textOutline");
            Writer.WriteAttributeString("opacity", item.TextOutline.Opacity.ToString(System.Globalization.CultureInfo.InvariantCulture));
            Writer.WriteAttributeString("colour", item.TextOutline.Color.ToArgb().ToString());
            Writer.WriteAttributeString("thickness", item.TextOutline.Thickness.ToString());
            Writer.WriteEndElement();

            Writer.WriteStartElement("dropShadow");
            Writer.WriteAttributeString("opacity", item.DropShadow.Opacity.ToString(System.Globalization.CultureInfo.InvariantCulture));
            Writer.WriteAttributeString("colour", item.DropShadow.Color.ToArgb().ToString());
            Writer.WriteAttributeString("angle", item.DropShadow.Direction.ToString());
            Writer.WriteAttributeString("altitude", item.DropShadow.Altitude.ToString());
            Writer.WriteEndElement();

            Writer.WriteEndElement();


        }

        public void LoadXml(System.Xml.XmlNode Settings)
        {
            System.Xml.XmlNode nameXml = Settings.SelectSingleNode("style/@name");
            System.Xml.XmlNode transparentNode = Settings.SelectSingleNode("style/@transparent");
            System.Xml.XmlNode backColourNode = Settings.SelectSingleNode("style/@backColour");
            System.Xml.XmlNode backImageNode = Settings.SelectSingleNode("style/@backImage");
            System.Xml.XmlNode transitionTimeNode = Settings.SelectSingleNode("style/@transitionTime");

            if (nameXml != null) Name = nameXml.Value;
            if (transparentNode != null) BackgroundTransparent = bool.Parse(transparentNode.Value);
            if (backColourNode != null) BackgroundColour = Color.FromArgb(int.Parse(backColourNode.Value));
            if (backImageNode != null) BackgroundImage = backImageNode.Value;
            if (transitionTimeNode != null) TransitionTime = int.Parse(transitionTimeNode.Value);

            LoadItemText(Settings.SelectSingleNode("style/mainText"), MainText);
            LoadItemText(Settings.SelectSingleNode("style/copyrightText"), Copyright);
        }

        private void LoadItemText(System.Xml.XmlNode itemNode, DisplayStyleTextItem textItem)
        {
            if (itemNode != null)
            {
                //Load the standard styles

                System.Xml.XmlNode textColourNode = itemNode.SelectSingleNode("@colour");
                System.Xml.XmlNode textFontNode = itemNode.SelectSingleNode("@fontFamily");
                System.Xml.XmlNode textFontSizeNode = itemNode.SelectSingleNode("@fontSize");
                System.Xml.XmlNode textFontStyleNode = itemNode.SelectSingleNode("@fontStyle");
                System.Xml.XmlNode alignmentNode = itemNode.SelectSingleNode("@alignment");
                System.Xml.XmlNode marginNode = itemNode.SelectSingleNode("@margin");

                if (textColourNode != null) textItem.ForeColour = Color.FromArgb(int.Parse(textColourNode.Value));
                if (textFontNode != null)
                {
                    FontStyle style = FontStyle.Regular;
                    if (textFontStyleNode != null)
                    {
                        style= (FontStyle)Enum.Parse(typeof(FontStyle), textFontStyleNode.Value);

                    }
                    textItem.Font = new Font(textFontNode.Value, float.Parse(textFontSizeNode.Value, System.Globalization.CultureInfo.InvariantCulture), style);
                }
                if (alignmentNode != null) textItem.Alignment = (ContentAlignment)Enum.Parse(typeof(ContentAlignment), alignmentNode.Value);
                if (marginNode != null) textItem.Margin = PaddingF.Parse(marginNode.Value);

                //Load Text Outline Style
                System.Xml.XmlNode outlineOpacityNode = itemNode.SelectSingleNode("textOutline/@opacity");
                System.Xml.XmlNode outlineColourNode = itemNode.SelectSingleNode("textOutline/@colour");
                System.Xml.XmlNode outlineThicknessNode = itemNode.SelectSingleNode("textOutline/@thickness");
                
                if (outlineOpacityNode != null) textItem.TextOutline.Opacity = float.Parse(outlineOpacityNode.Value,System.Globalization.CultureInfo.InvariantCulture);
                if (outlineColourNode != null) textItem.TextOutline.Color = Color.FromArgb(int.Parse(outlineColourNode.Value)); 
                if (outlineThicknessNode != null) textItem.TextOutline.Thickness = int.Parse(outlineThicknessNode.Value);

                //Load Drop Shadow Style
                System.Xml.XmlNode shadowOpacityNode = itemNode.SelectSingleNode("dropShadow/@opacity");
                System.Xml.XmlNode shadowColourNode = itemNode.SelectSingleNode("dropShadow/@colour");
                System.Xml.XmlNode shadowAngleNode = itemNode.SelectSingleNode("dropShadow/@angle");
                System.Xml.XmlNode shadowAltitudeNode = itemNode.SelectSingleNode("dropShadow/@altitude");

                if (shadowOpacityNode != null) textItem.DropShadow.Opacity = float.Parse(shadowOpacityNode.Value, System.Globalization.CultureInfo.InvariantCulture);
                if (shadowColourNode != null) textItem.DropShadow.Color = Color.FromArgb(int.Parse(shadowColourNode.Value));
                if (shadowAngleNode != null) textItem.DropShadow.Direction = int.Parse(shadowAngleNode.Value);
                if (shadowAltitudeNode != null) textItem.DropShadow.Altitude = int.Parse(shadowAltitudeNode.Value);
            }
        }

        #region ICloneable Members

        public override object Clone()
        {
            return new SongDisplayStyle(this);
        }

        #endregion
    }
}
